home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / GNU_KIT / DISK8.ZIP / src / makest / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-08  |  33.4 KB  |  1,365 lines

  1. /* Copyright (C) 1988-1991 Free Software Foundation, Inc.
  2. This file is part of GNU Make.
  3.  
  4. GNU Make is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 1, or (at your option)
  7. any later version.
  8.  
  9. GNU Make is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with GNU Make; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include "make.h"
  19. #include "commands.h"
  20. #include "dep.h"
  21. #include "file.h"
  22. #include "variable.h"
  23. #include "job.h"
  24. #include <ctype.h>
  25. #include <time.h>
  26.  
  27.  
  28. extern char *version_string;
  29.  
  30. extern int fatal_error_signal ();
  31.  
  32. extern struct dep *read_all_makefiles ();
  33.  
  34. extern void print_variable_data_base ();
  35. extern void print_dir_data_base ();
  36. extern void print_rule_data_base ();
  37. extern void print_file_data_base ();
  38. extern void print_vpath_data_base ();
  39.  
  40. #if    !defined(__GNU_LIBRARY__) && !defined(_POSIX_SOURCE)
  41. extern int chdir ();
  42. extern void exit ();
  43. extern time_t time ();
  44. extern double atof ();
  45. #endif
  46. extern char *mktemp ();
  47.  
  48. static void log_working_directory ();
  49. static void print_data_base (), print_version ();
  50. static void decode_switches (), decode_env_switches ();
  51. static void define_makeflags ();
  52.  
  53.  
  54. #if 0 /* dummy tag */
  55. flags () {}
  56. #endif
  57. /* Flags:
  58.  *    -b ignored for compatibility with System V Make
  59.  *    -C change directory
  60.  *    -d debug
  61.  *    -e env_overrides
  62.  *    -f makefile
  63.  *    -i ignore_errors
  64.  *    -j job_slots
  65.  *    -k keep_going
  66.  *    -l max_load_average
  67.  *    -m ignored for compatibility with something or other
  68.  *    -n just_print
  69.  *    -o consider file old
  70.  *    -p print_data_base
  71.  *    -q question
  72.  *    -r no_builtin_rules
  73.  *    -s silent
  74.  *    -S turn off -k
  75.  *    -t touch
  76.  *    -v print version information
  77.  *    -w log working directory
  78.  *    -W consider file new (with -n, `what' if effect)
  79.  */
  80.  
  81. /* The structure that describes an accepted command switch.  */
  82.  
  83. struct command_switch
  84.   {
  85.     char c;        /* The switch character.  */
  86.     enum        /* Type of the value.  */
  87.       {
  88.     flag,        /* Turn int flag on.  */
  89.     flag_off,    /* Turn int flag off.  */
  90.     string,        /* One string per switch, may be in the same word.  */
  91.     positive_int,    /* A positive integer.  */
  92.     floating,    /* A floating-point number (double).  */
  93.     ignore        /* Ignored.  */
  94.       } type;
  95.  
  96.     char *value_ptr;    /* Pointer to the value-holding variable.  */
  97.  
  98.     unsigned int env:1;        /* Can come from MAKEFLAGS.  */
  99.     unsigned int toenv:1;    /* Should be put in MAKEFLAGS.  */
  100.     unsigned int no_makefile:1;    /* Don't propagate when remaking makefiles.  */
  101.  
  102.     char *noarg_value;    /* Pointer to value used if no argument is given.  */
  103.     char *default_value;/* Pointer to default value.  */
  104.   };
  105.  
  106.  
  107. /* The structure used to hold the list of strings given
  108.    in command switches of a type that takes string arguments.  */
  109.  
  110. struct stringlist
  111.   {
  112.     char **list;    /* Nil-terminated list of strings.  */
  113.     unsigned int idx;    /* Index into above.  */
  114.     unsigned int max;    /* Number of pointers allocated.  */
  115.   };
  116.  
  117.  
  118. /* The recognized command switches.  */
  119.  
  120. /* Nonzero means do not print commands to be executed (-s).  */
  121.  
  122. int silent_flag;
  123.  
  124. /* Nonzero means just touch the files
  125.    that would appear to need remaking (-t)  */
  126.  
  127. int touch_flag;
  128.  
  129. /* Nonzero means just print what commands would need to be executed,
  130.    don't actually execute them (-n).  */
  131.  
  132. int just_print_flag;
  133.  
  134. /* Print debugging trace info (-d).  */
  135.  
  136. int debug_flag = 0;
  137.  
  138. /* Environment variables override makefile definitions.  */
  139.  
  140. int env_overrides = 0;
  141.  
  142. /* Nonzero means ignore status codes returned by commands
  143.    executed to remake files.  Just treat them all as successful (-i).  */
  144.  
  145. int ignore_errors_flag = 0;
  146.  
  147. /* Nonzero means don't remake anything, just print the data base
  148.    that results from reading the makefile (-p).  */
  149.  
  150. int print_data_base_flag = 0;
  151.  
  152. /* Nonzero means don't remake anything; just return a nonzero status
  153.    if the specified targets are not up to date (-q).  */
  154.  
  155. int question_flag = 0;
  156.  
  157. /* Nonzero means do not use any of the builtin rules (-r).  */
  158.  
  159. int no_builtin_rules_flag = 0;
  160.  
  161. /* Nonzero means keep going even if remaking some file fails (-k).  */
  162.  
  163. int keep_going_flag;
  164. int default_keep_going_flag = 0;
  165.  
  166. /* Nonzero means print working directory
  167.    before starting and after finishing make. */
  168.  
  169. int print_directory_flag = 0;
  170.  
  171. /* Nonzero means print version information.  */
  172.  
  173. int print_version_flag = 0;
  174.  
  175. /* List of makefiles given with -f switches.  */
  176.  
  177. static struct stringlist *makefiles = 0;
  178.  
  179.  
  180. /* Number of job slots (commands that can be run at once).  */
  181.  
  182. unsigned int job_slots = 1;
  183. unsigned int default_job_slots = 1;
  184.  
  185. /* Value of job_slots that means no limit.  */
  186.  
  187. static unsigned int inf_jobs = 0;
  188.  
  189. /* Maximum load average at which multiple jobs will be run.
  190.    Negative values mean unlimited, while zero means limit to
  191.    zero load (which could be useful to start infinite jobs remotely
  192.    but one at a time locally).  */
  193.  
  194. double max_load_average = -1.0;
  195. double default_load_average = -1.0;
  196.  
  197. /* List of directories given with -c switches.  */
  198.  
  199. static struct stringlist *directories = 0;
  200.  
  201. /* List of include directories given with -I switches.  */
  202.  
  203. static struct stringlist *include_directories = 0;
  204.  
  205. /* List of files given with -o switches.  */
  206.  
  207. static struct stringlist *old_files = 0;
  208.  
  209. /* List of files given with -W switches.  */
  210.  
  211. static struct stringlist *new_files = 0;
  212.  
  213. /* The table of command switches.  */
  214.  
  215. static struct command_switch switches[] =
  216.   {
  217.     { 'b', ignore, 0, 0, 0, 0, 0, 0 },
  218.     { 'C', string, (char *) &directories, 0, 0, 0, 0, 0 },
  219.     { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0 },
  220.     { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0 },
  221.     { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0 },
  222.     { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0 },
  223.     { 'I', string, (char *) &include_directories, 0, 0, 0, 0, 0 },
  224.     { 'j', positive_int, (char *) &job_slots, 1, 1, 0,
  225.     (char *) &inf_jobs, (char *) &default_job_slots },
  226.     { 'k', flag, (char *) &keep_going_flag, 1, 1, 0,
  227.     0, (char *) &default_keep_going_flag },
  228.     { 'l', floating, (char *) &max_load_average, 1, 1, 0,
  229.     (char *) &default_load_average, (char *) &default_load_average },
  230.     { 'm', ignore, 0, 0, 0, 0, 0, 0 },
  231.     { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0 },
  232.     { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0 },
  233.     { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0 },
  234.     { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0 },
  235.     { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0 },
  236.     { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0 },
  237.     { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0,
  238.     0, (char *) &default_keep_going_flag },
  239.     { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0 },
  240.     { 'v', flag, (char *) &print_version_flag, 0, 0, 0, 0, 0 },
  241.     { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0 },
  242.     { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0 },
  243.     { '\0', ignore, 0, 0, 0, 0, 0 }
  244.   };
  245.  
  246. /* List of non-switch arguments.  */
  247.  
  248. struct stringlist *other_args = 0;
  249.  
  250.  
  251. /* The name we were invoked with.  */
  252.  
  253. char *program;
  254.  
  255. /* Value of the MAKELEVEL variable at startup (or 0).  */
  256.  
  257. unsigned int makelevel;
  258.  
  259. /* First file defined in the makefile whose name does not
  260.    start with `.'.  This is the default to remake if the
  261.    command line does not specify.  */
  262.  
  263. struct file *default_goal_file;
  264.  
  265. /* Pointer to structure for the file .DEFAULT
  266.    whose commands are used for any file that has none of its own.
  267.    This is zero if the makefiles do not define .DEFAULT.  */
  268.  
  269. struct file *default_file;
  270.  
  271. /* Mask of signals that are being caught with fatal_error_signal.  */
  272. int fatal_signal_mask;
  273.  
  274. int
  275. main (argc, argv, envp)
  276.      int argc;
  277.      char **argv;
  278.      char **envp;
  279. {
  280. #if    (defined(USG) && !defined(HAVE_SIGLIST)) || defined(DGUX)
  281.   extern void init_siglist ();
  282. #endif
  283.   extern int child_handler ();
  284.   register struct file *f;
  285.   register unsigned int i;
  286.   register char *cmd_defs;
  287.   register unsigned int cmd_defs_len, cmd_defs_idx;
  288.   char **p;
  289.   time_t now;
  290.   struct dep *goals = 0;
  291.   register struct dep *lastgoal;
  292.   struct dep *read_makefiles;
  293.   char current_directory[MAXPATHLEN];
  294.  
  295. #ifdef atarist
  296.   _binmode( 0 );
  297. #endif /* atarist */
  298.  
  299.   default_goal_file = 0;
  300.   reading_filename = 0;
  301.   reading_lineno_ptr = 0;
  302.   
  303. #if    (defined(USG) && !defined(HAVE_SIGLIST)) || defined(DGUX)
  304.   init_siglist ();
  305. #endif
  306.  
  307.   fatal_signal_mask = 0;
  308.  
  309. #define    FATAL_SIG(sig)                                  \
  310.   if (SIGNAL ((sig), (SIGHANDLER) fatal_error_signal)                  \
  311.       == (SIGHANDLER) SIG_IGN)                              \
  312.     (void) SIGNAL ((sig), SIG_IGN);                          \
  313.   else                                          \
  314.     fatal_signal_mask |= sigmask (sig);
  315.  
  316.   FATAL_SIG (SIGHUP);
  317.   FATAL_SIG (SIGQUIT);
  318.   FATAL_SIG (SIGINT);
  319.   FATAL_SIG (SIGILL);
  320.   FATAL_SIG (SIGTRAP);
  321.   FATAL_SIG (SIGIOT);
  322. #ifdef    SIGEMT
  323.   FATAL_SIG (SIGEMT);
  324. #endif
  325. #ifdef    SIGDANGER
  326.   FATAL_SIG (SIGDANGER);
  327. #endif
  328.   FATAL_SIG (SIGFPE);
  329.   FATAL_SIG (SIGBUS);
  330.   FATAL_SIG (SIGSEGV);
  331.   FATAL_SIG (SIGSYS);
  332.   FATAL_SIG (SIGTERM);
  333. #ifdef SIGXCPU
  334.   FATAL_SIG (SIGXCPU);
  335. #endif
  336. #ifdef SIGXFSZ
  337.   FATAL_SIG (SIGXFSZ);
  338. #endif
  339.  
  340. #undef    FATAL_SIG
  341.  
  342.   /* Make sure stdout is line-buffered.  */
  343.  
  344. #if    (defined (USGr3) || defined (HPUX) || defined (hpux) \
  345.      || defined (M_XENIX) || defined (APOLLO))
  346.   setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
  347. #else    /* Not USGr3 and not HPUX et al.  */
  348. #ifdef    USG
  349.   setvbuf (stdout, _IOLBF, (char *) 0, BUFSIZ);
  350. #else    /* Not USG.  */
  351.   setlinebuf (stdout);
  352. #endif    /* USG.  */
  353. #endif    /* USGr3.  */
  354.  
  355.   /* Set up to access user data (files).  */
  356.   user_access ();
  357.  
  358.   /* Figure out where this program lives.  */
  359.  
  360.   if (argv[0] == 0)
  361.     argv[0] = "";
  362.   if (argv[0][0] == '\0')
  363.     program = "make";
  364.   else 
  365.     {
  366.       program = rindex (argv[0], '/');
  367.       if (program == 0)
  368.     program = argv[0];
  369.       else
  370.     ++program;
  371.     }
  372.  
  373.   /* Figure out where we are.  */
  374.  
  375. #ifdef    USG
  376.   /* In some System V's, `getcwd' spawns a child running /bin/pwd.  */
  377.   push_signals_blocked_p (1);
  378. #endif
  379.   if (getwd (current_directory) == 0)
  380.     {
  381.       error ("getwd: %s", current_directory);
  382.       current_directory[0] = '\0';
  383.     }
  384. #ifdef    USG
  385.   pop_signals_blocked_p ();
  386. #endif
  387.  
  388.   /* Read in variables from the environment.  It is important that this be
  389.      done before `MAKE' and `MAKEOVERRIDES' are figured out so their
  390.      definitions will not be ones from the environment.  */
  391.  
  392.   for (i = 0; envp[i] != 0; ++i)
  393.     {
  394.       register char *ep = envp[i];
  395.       while (*ep++ != '=')
  396.     ;
  397.       (void) define_variable (envp[i], ep - envp[i] - 1, ep, o_env, 1);
  398.     }
  399.  
  400.   /* Decode the switches.  */
  401.  
  402.   decode_switches (argc, argv);
  403.  
  404.   /* Print version information.  */
  405.  
  406.   if (print_version_flag || print_data_base_flag || debug_flag)
  407.     print_version ();
  408.  
  409.   /* Search for command line arguments that define variables,
  410.      and do the definitions.  Also save up the text of these
  411.      arguments in CMD_DEFS so we can put them into the values
  412.      of $(MAKEOVERRIDES) and $(MAKE).  */
  413.  
  414.   cmd_defs_len = 200;
  415.   cmd_defs = (char *) xmalloc (cmd_defs_len);
  416.   cmd_defs_idx = 0;
  417.  
  418.   for (i = 1; other_args->list[i] != 0; ++i)
  419.     {
  420.       if (other_args->list[i][0] == '\0')
  421.     /* Ignore empty arguments, so they can't kill enter_file.  */
  422.     continue;
  423.  
  424.       /* Try a variable definition.  */
  425.       if (try_variable_definition (other_args->list[i], o_command))
  426.     {
  427.       /* It succeeded.  The variable is already defined.
  428.          Backslash-quotify it and append it to CMD_DEFS, then clobber it
  429.          to 0 in the list so that it won't be taken for a goal target.  */
  430.       register char *p = other_args->list[i];
  431.       unsigned int l = strlen (p);
  432.       if (cmd_defs_idx + (l * 2) + 1 > cmd_defs_len)
  433.         {
  434.           if (l * 2 > cmd_defs_len)
  435.         cmd_defs_len += l * 2;
  436.           else
  437.         cmd_defs_len *= 2;
  438.           cmd_defs = (char *) xrealloc (cmd_defs, cmd_defs_len);
  439.         }
  440.       
  441.       while (*p != '\0')
  442.         {
  443.           if (index ("'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *p) != 0)
  444.         cmd_defs[cmd_defs_idx++] = '\\';
  445.           cmd_defs[cmd_defs_idx++] = *p++;
  446.         }
  447.       cmd_defs[cmd_defs_idx++] = ' ';
  448.     }
  449.       else
  450.     {
  451.       /* It was not a variable definition, so it is a target to be made.
  452.          Enter it as a file and add it to the dep chain of goals.  */
  453.       f = enter_file (other_args->list[i]);
  454.       f->cmd_target = 1;
  455.       
  456.       if (goals == 0)
  457.         {
  458.           goals = (struct dep *) xmalloc (sizeof (struct dep));
  459.           lastgoal = goals;
  460.         }
  461.       else
  462.         {
  463.           lastgoal->next = (struct dep *) xmalloc (sizeof (struct dep));
  464.           lastgoal = lastgoal->next;
  465.         }
  466.       lastgoal->name = 0;
  467.       lastgoal->file = f;
  468.     }
  469.     }
  470.  
  471.   if (cmd_defs_idx > 0)
  472.     {
  473.       cmd_defs[cmd_defs_idx - 1] = '\0';
  474.       (void) define_variable ("MAKEOVERRIDES", 13, cmd_defs, o_override, 0);
  475.     }
  476.  
  477.   /* Set the "MAKE" variable to the name we were invoked with.
  478.      (If it is a relative pathname with a slash, prepend our directory name
  479.      so the result will run the same program regardless of the current dir.
  480.      If it is a name with no slash, we can only hope that PATH did not
  481.      find it in the current directory.)
  482.  
  483.      Append the command-line variable definitions gathered above
  484.      so sub-makes will get them as command-line definitions.  */
  485.  
  486.   if (current_directory[0] != '\0'
  487.       && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
  488.     argv[0] = concat (current_directory, "/", argv[0]);
  489.  
  490.   if (cmd_defs_idx > 0)
  491.     {
  492.       char *str = concat (argv[0], " ", cmd_defs);
  493.       (void) define_variable ("MAKE", 4, str, o_env, 0);
  494.       free (str);
  495.     }
  496.   else
  497.     (void) define_variable ("MAKE", 4, argv[0], o_env, 0);
  498.  
  499.   free (cmd_defs);
  500.  
  501.   /* If there were -c flags, move ourselves about.  */
  502.  
  503.   if (directories != 0)
  504.     for (i = 0; directories->list[i] != 0; ++i)
  505.       {
  506.     char *dir = directories->list[i];
  507.     if (chdir (dir) < 0)
  508.       pfatal_with_name (dir);
  509.       }
  510.  
  511.   /* Figure out the level of recursion.  */
  512.   {
  513.     struct variable *v = lookup_variable ("MAKELEVEL", 9);
  514.     if (v != 0 && *v->value != '\0' && *v->value != '-')
  515.       makelevel = (unsigned int) atoi (v->value);
  516.     else
  517.       makelevel = 0;
  518.   }
  519.  
  520.   /* Construct the list of include directories to search.  */
  521.  
  522.   construct_include_path (include_directories == 0 ? (char **) 0
  523.               : include_directories->list);
  524.  
  525.   /* Tell the user where he is.  */
  526.  
  527.   if (print_directory_flag)
  528.     log_working_directory (1);
  529.  
  530.   /* Read any stdin makefiles into temporary files.  */
  531.  
  532.   if (makefiles != 0)
  533.     {
  534.       register unsigned int i;
  535.       for (i = 0; i < makefiles->idx; ++i)
  536.     if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
  537.       {
  538.         /* This makefile is standard input.  Since we may re-exec
  539.            and thus re-read the makefiles, we read standard input
  540.            into a temporary file and read from that.  */
  541.         static char name[] = "/tmp/GmXXXXXX";
  542.         FILE *outfile;
  543.  
  544.         /* Free the storage allocated for "-".  */
  545.         free (makefiles->list[i]);
  546.  
  547.         /* Make a unique filename.  */
  548.         (void) mktemp (name);
  549.  
  550.         outfile = fopen (name, "w");
  551.         if (outfile == 0)
  552.           pfatal_with_name ("fopen (temporary file)");
  553.         while (!feof (stdin))
  554.           {
  555.         char buf[2048];
  556.         int n = fread (buf, 1, sizeof(buf), stdin);
  557.         if (n > 0 && fwrite (buf, 1, n, outfile) != n)
  558.           pfatal_with_name ("fwrite (temporary file)");
  559.           }
  560.         /* Try to make sure we won't remake the temporary
  561.            file when we are re-exec'd.  Kludge-o-matic!  */
  562.         fprintf (outfile, "%s:;\n", name);
  563.         (void) fclose (outfile);
  564.  
  565.         /* Replace the name that read_all_makefiles will
  566.            see with the name of the temporary file.  */
  567.         makefiles->list[i] = savestring (name, sizeof name - 1);
  568.  
  569.         /* Make sure the temporary file will not be remade.  */
  570.         f = enter_file (savestring (name, sizeof name - 1));
  571.         f->updated = 1;
  572.         f->update_status = 0;
  573.         /* Let it be removed when we're done.  */
  574.         f->intermediate = 1;
  575.         /* But don't mention it.  */
  576.         f->dontcare = 1;
  577.       }
  578.     }
  579.  
  580.   /* Set up to handle children dying.  This must be done before
  581.      reading in the makefiles so that `shell' function calls will work.  */
  582.  
  583. #if    defined (SIGCHLD) && !defined (USG)
  584.   (void) SIGNAL (SIGCHLD, child_handler);
  585. #else
  586.   (void) SIGNAL (SIGCLD, child_handler);
  587. #endif
  588.  
  589.   /* Define the initial list of suffixes for old-style rules.  */
  590.  
  591.   set_default_suffixes ();
  592.  
  593.   /* Define some internal and special variables.  */
  594.  
  595.   define_automatic_variables ();
  596.  
  597.   /* Set up the MAKEFLAGS and MFLAGS variables
  598.      so makefiles can look at them.  */
  599.  
  600.   define_makeflags (0, 0);
  601.  
  602.   /* Read all the makefiles.  */
  603.  
  604.   default_file = enter_file (".DEFAULT");
  605.  
  606.   read_makefiles
  607.     = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
  608.  
  609.   decode_env_switches ("MAKEFLAGS", 9);
  610.   decode_env_switches ("MFLAGS", 6);
  611.  
  612.   /* Set up MAKEFLAGS and MFLAGS again, so they will be right.  */
  613.  
  614.   define_makeflags (1, 0);
  615.  
  616.   ignore_errors_flag |= lookup_file (".IGNORE") != 0;
  617.  
  618.   silent_flag |= lookup_file (".SILENT") != 0;
  619.  
  620.   /* Make each `struct dep' point at the
  621.      `struct file' for the file depended on.  */
  622.  
  623.   snap_deps ();
  624.  
  625.   /* Install the default implicit rules.
  626.      This used to be done before reading the makefiles.
  627.      But in that case, built-in pattern rules were in the chain
  628.      before user-defined ones, so they matched first.  */
  629.  
  630.   install_default_implicit_rules ();
  631.  
  632.   /* Convert old-style suffix rules to pattern rules.  */
  633.  
  634.   convert_to_pattern ();
  635.  
  636.   /* Compute implicit rule limits.  */
  637.  
  638.   count_implicit_rule_limits ();
  639.  
  640.   /* Construct the listings of directories in VPATH lists.  */
  641.  
  642.   build_vpath_lists ();
  643.  
  644.   /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
  645.      and as having been updated already, and files given with -W flags
  646.      as brand new (time-stamp of now).  */
  647.  
  648.   if (old_files != 0)
  649.     for (p = old_files->list; *p != 0; ++p)
  650.       {
  651.     f = enter_file (*p);
  652.     f->last_mtime = (time_t) 1;
  653.     f->updated = 1;
  654.     f->update_status = 0;
  655.       }
  656.  
  657.   if (new_files != 0)
  658.     {
  659.       now = time ((time_t *) 0);
  660.       for (p = new_files->list; *p != 0; ++p)
  661.     {
  662.       f = enter_file (*p);
  663.       f->last_mtime = now;
  664.     }
  665.     }
  666.  
  667.   if (read_makefiles != 0)
  668.     {
  669.       /* Update any makefiles if necessary.  */
  670.  
  671.       time_t *makefile_mtimes = 0;
  672.       unsigned int mm_idx = 0;
  673.  
  674.       if (debug_flag)
  675.     puts ("Updating makefiles....");
  676.  
  677.       /* Remove any makefiles we don't want to try to update.
  678.      Also record the current modtimes so we can compare them later.  */
  679.       {
  680.     register struct dep *d, *last;
  681.     last = 0;
  682.     d = read_makefiles;
  683.     while (d != 0)
  684.       {
  685.         register struct file *f = d->file;
  686.         if (f->is_target && f->deps == 0 && f->cmds != 0)
  687.           {
  688.         /* This makefile is a target with commands, but has no
  689.            dependencies.  So, it will always be remade.
  690.            This might well cause an infinite loop, so don't try to
  691.            remake it.  (This will only happen if your makefiles are
  692.            written exceptionally stupidly; but if you work for Athena,
  693.            that's how you write your makefiles.)  */
  694.  
  695.         if (debug_flag)
  696.           printf ("Makefile `%s' might loop; not remaking it.\n",
  697.               f->name);
  698.  
  699.         if (last == 0)
  700.           read_makefiles = d->next;
  701.         else
  702.           last->next = d->next;
  703.  
  704.         /* Free the storage.  */
  705.         free ((char *) d);
  706.  
  707.         d = last == 0 ? 0 : last->next;
  708.           }
  709.         else
  710.           {
  711.         if (makefile_mtimes == 0)
  712.           makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
  713.         else
  714.           makefile_mtimes = (time_t *)
  715.             xrealloc ((char *) makefile_mtimes,
  716.                   (mm_idx + 1) * sizeof (time_t));
  717.         makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
  718.         last = d;
  719.         d = d->next;
  720.           }
  721.       }
  722.       }    
  723.  
  724.       /* Set up `MAKEFLAGS' specially while remaking makefiles.  */
  725.       define_makeflags (1, 1);
  726.  
  727.       switch (update_goal_chain (read_makefiles, 1))
  728.     {
  729.     default:
  730.       abort ();
  731.       
  732.     case -1:
  733.       /* Did nothing.  */
  734.       break;
  735.       
  736.     case 1:
  737.       /* Failed to update.  Figure out if we care.  */
  738.       {
  739.         /* Nonzero if any makefile was successfully remade.  */
  740.         int any_remade = 0;
  741.         /* Nonzero if any makefile we care about failed
  742.            in updating or could not be found at all.  */
  743.         int any_failed = 0;
  744.         register unsigned int i;
  745.  
  746.         for (i = 0; read_makefiles != 0; ++i)
  747.           {
  748.         struct dep *d = read_makefiles;
  749.         read_makefiles = d->next;
  750.         if (d->file->updated)
  751.           {
  752.             /* This makefile was updated.  */
  753.             if (d->file->update_status == 0)
  754.               {
  755.             /* It was successfully updated.  */
  756.             any_remade |= (file_mtime_no_search (d->file)
  757.                        != makefile_mtimes[i]);
  758.               }
  759.             else if (d->changed != 1)
  760.               {
  761.             time_t mtime;
  762.             /* The update failed and this makefile was not
  763.                from the MAKEFILES variable, so we care.  */
  764.             error ("Failed to remake makefile `%s'.",
  765.                    d->file->name);
  766.             mtime = file_mtime_no_search (d->file);
  767.             any_remade |= (mtime != (time_t) -1
  768.                        && mtime != makefile_mtimes[i]);
  769.               }
  770.           }
  771.         else
  772.           /* This makefile was not found at all.  */
  773.           switch (d->changed)
  774.             {
  775.             case 0:
  776.               /* A normal makefile.  We must die later.  */
  777.               error ("Makefile `%s' was not found", dep_name (d));
  778.               any_failed = 1;
  779.               break;
  780.             case 1:
  781.               /* A makefile from the MAKEFILES variable.
  782.              We don't care.  */
  783.               break;
  784.             case 2:
  785.               /* An included makefile.  We don't need
  786.              to die, but we do want to complain.  */
  787.               error ("Included makefile `%s' was not found.",
  788.                  dep_name (d));
  789.               break;
  790.             }
  791.  
  792.         free ((char *) d);
  793.           }
  794.  
  795.         if (any_remade)
  796.           goto re_exec;
  797.         else if (any_failed)
  798.           die (1);
  799.         else
  800.           break;
  801.       }
  802.  
  803.     case 0:
  804.     re_exec:;
  805.       /* Updated successfully.  Re-exec ourselves.  */
  806.       if (print_directory_flag)
  807.         log_working_directory (0);
  808.       if (debug_flag)
  809.         puts ("Re-execing myself....");
  810.       if (makefiles != 0)
  811.         {
  812.           /* These names might have changed.  */
  813.           register unsigned int i, j = 0;
  814.           for (i = 1; i < argc; ++i)
  815.         if (!strcmp (argv[i], "-f"))
  816.             {
  817.               char *p = &argv[i][2];
  818.               if (*p == '\0')
  819.             argv[++i] = makefiles->list[j];
  820.               else
  821.             argv[i] = concat ("-f", makefiles->list[j], "");
  822.               ++j;
  823.             }
  824.         }
  825.       if (directories != 0 && directories->idx > 0)
  826.         {
  827.           char bad;
  828.           if (current_directory[0] != '\0')
  829.         {
  830.           if (chdir (current_directory) < 0)
  831.             {
  832.               perror_with_name ("chdir", "");
  833.               bad = 1;
  834.             }
  835.           else
  836.             bad = 0;
  837.         }
  838.           else
  839.         bad = 1;
  840.           if (bad)
  841.         fatal ("Couldn't change back to original directory.");
  842.         }
  843.       fflush (stdout);
  844.       fflush (stderr);
  845.       for (p = environ; *p != 0; ++p)
  846.         if (!strncmp (*p, "MAKELEVEL=", 10))
  847.           {
  848.         sprintf (*p, "MAKELEVEL=%u", makelevel);
  849.         break;
  850.           }
  851.       exec_command (argv, environ);
  852.       /* NOTREACHED */
  853.     }
  854.     }
  855.  
  856.   /* Set up `MAKEFLAGS' again for the normal targets.  */
  857.   define_makeflags (1, 0);
  858.  
  859.   {
  860.     int status;
  861.  
  862.     /* If there were no command-line goals, use the default.  */
  863.     if (goals == 0)
  864.       {
  865.     if (default_goal_file != 0)
  866.       {
  867.         goals = (struct dep *) xmalloc (sizeof (struct dep));
  868.         goals->next = 0;
  869.         goals->name = 0;
  870.         goals->file = default_goal_file;
  871.       }
  872.       }
  873.     else
  874.       lastgoal->next = 0;
  875.  
  876.     if (goals != 0)
  877.       {
  878.     /* Update the goals.  */
  879.  
  880.     if (debug_flag)
  881.       puts ("Updating goal targets....");
  882.  
  883.     switch (update_goal_chain (goals, 0))
  884.       {
  885.       case -1:
  886.         /* Nothing happened.  */
  887.       case 0:
  888.         /* Updated successfully.  */
  889.         status = 0;
  890.         break;
  891.       case 1:
  892.         /* Updating failed.  */
  893.         status = 1;
  894.         break;
  895.       default:
  896.         abort ();
  897.       }
  898.       }
  899.  
  900.     /* Print the data base under -p.  */
  901.     if (print_data_base_flag)
  902.       print_data_base ();
  903.  
  904.     if (goals == 0)
  905.       {
  906.     if (read_makefiles == 0)
  907.       fatal ("No targets specified and no makefile found");
  908.     else
  909.       fatal ("No targets");
  910.       }
  911.  
  912.     /* Exit.  */
  913.     die (status);
  914.   }
  915.  
  916.   return 0;
  917. }
  918.  
  919. static void
  920. decode_switches (argc, argv)
  921.      int argc;
  922.      char **argv;
  923. {
  924.   char bad = 0;
  925.   register unsigned int i;
  926.   register char *sw;
  927.   register struct command_switch *cs;
  928.   register struct stringlist *sl;
  929.   char *arg;
  930.  
  931.   decode_env_switches ("MAKEFLAGS", 9);
  932.   decode_env_switches ("MFLAGS", 6);
  933.  
  934.   other_args = (struct stringlist *) xmalloc (sizeof (struct stringlist));
  935.   other_args->max = 5;
  936.   other_args->list = (char **) xmalloc (5 * sizeof (char *));
  937.   other_args->idx = 1;
  938.   other_args->list[0] = savestring (argv[0], strlen (argv[0]));
  939.  
  940.   for (i = 1; i < argc; i++)
  941.     {
  942.       sw = argv[i];
  943.       if (*sw++ == '-')
  944.     while (*sw != '\0')
  945.       {
  946.         for (cs = switches; cs->c != '\0'; ++cs)
  947.           if (cs->c == *sw)
  948.         {
  949.           ++sw;
  950.           switch (cs->type)
  951.             {
  952.             default:
  953.               abort ();
  954.             case ignore:
  955.               break;
  956.             case flag:
  957.             case flag_off:
  958.               *(int *) cs->value_ptr = cs->type == flag;
  959.               break;
  960.             case string:
  961.               if (*sw == '\0')
  962.             {
  963.               arg = argv[++i];
  964.               if (arg == 0)
  965.                 {
  966.                   arg = cs->noarg_value;
  967.                   if (arg == 0)
  968.                 {
  969.                   error ("argument required for `-%c' option",
  970.                      cs->c);
  971.                   bad = 1;
  972.                   break;
  973.                 }
  974.                 }
  975.             }
  976.               else
  977.             arg = sw;
  978.  
  979.               sl = *(struct stringlist **) cs->value_ptr;
  980.               if (sl == 0)
  981.             {
  982.               sl = (struct stringlist *)
  983.                 xmalloc (sizeof (struct stringlist));
  984.               sl->max = 5;
  985.               sl->idx = 0;
  986.               sl->list = (char **) xmalloc (5 * sizeof (char *));
  987.               *(struct stringlist **) cs->value_ptr = sl;
  988.             }
  989.               else if (sl->idx == sl->max - 1)
  990.             {
  991.               sl->max += 5;
  992.               sl->list = (char **)
  993.                 xrealloc ((char *) sl->list,
  994.                       sl->max * sizeof (char *));
  995.             }
  996.               sl->list[sl->idx++] = savestring (arg, strlen (arg));
  997.               sl->list[sl->idx] = 0;
  998.               sw = "";
  999.               break;
  1000.  
  1001.             case positive_int:
  1002.               if (*sw == '\0')
  1003.             arg = argv[++i];
  1004.               else
  1005.             arg = sw;
  1006.               if (arg != 0 && isdigit (*arg))
  1007.             {
  1008.               int i = atoi (arg);
  1009.               if (arg == sw)
  1010.                 while (isdigit (*sw))
  1011.                   ++sw;
  1012.               if (i < 1)
  1013.                 {
  1014.                   error ("the `-%c' option requires a \
  1015. positive integral argument",
  1016.                      cs->c);
  1017.                   bad = 1;
  1018.                 }
  1019.               else
  1020.                 *(unsigned int *) cs->value_ptr = i;
  1021.             }
  1022.               else
  1023.             {
  1024.               if (cs->noarg_value != 0)
  1025.                 *(unsigned int *) cs->value_ptr
  1026.                   = *(unsigned int *) cs->noarg_value;
  1027.               else
  1028.                 {
  1029.                   error ("the `-%c' option requires an argument",
  1030.                      cs->c);
  1031.                   bad = 1;
  1032.                 }
  1033.  
  1034.               if (arg == argv[i])
  1035.                 /* We moved to the next arg, so move back.  */
  1036.                 --i;
  1037.             }
  1038.               break;
  1039.  
  1040.             case floating:
  1041.               if (*sw == '\0')
  1042.             arg = argv[++i];
  1043.               else
  1044.             arg = sw;
  1045.               if (arg != 0 && (*arg == '.' || isdigit (*arg)))
  1046.             {
  1047.               *(double *) cs->value_ptr = atof (arg);
  1048.               if (arg == sw)
  1049.                 while (*sw == '.' || isdigit (*sw))
  1050.                   ++sw;
  1051.             }
  1052.               else
  1053.             {
  1054.               if (cs->noarg_value != 0)
  1055.                 *(double *) cs->value_ptr
  1056.                   = *(double *) cs->noarg_value;
  1057.               else
  1058.                 {
  1059.                   error ("the `-%c' option requires an argument",
  1060.                      cs->c);
  1061.                   bad = 1;
  1062.                 }
  1063.  
  1064.               if (arg == argv[i])
  1065.                 /* We moved to the next arg, so move back.  */
  1066.                 --i;
  1067.             }
  1068.               break;
  1069.             }
  1070.  
  1071.           /* We've found the switch.  Stop looking.  */
  1072.           break;
  1073.         }
  1074.           
  1075.         if (cs->c == '\0')
  1076.           {
  1077.         error ("unknown option `-%c'", *sw++);
  1078.         bad = 1;
  1079.           }
  1080.       }
  1081.       else
  1082.     {
  1083.       if (other_args->idx == other_args->max - 1)
  1084.         {
  1085.           other_args->max += 5;
  1086.           other_args->list = (char **)
  1087.         xrealloc ((char *) other_args->list,
  1088.               other_args->max * sizeof (char *));
  1089.         }
  1090.       other_args->list[other_args->idx++] = argv[i];
  1091.     }
  1092.     }
  1093.  
  1094.   if (other_args != 0)
  1095.     other_args->list[other_args->idx] = 0;
  1096.  
  1097.   if (bad)
  1098.     die (1);
  1099. }
  1100.  
  1101. static void
  1102. decode_env_switches (envar, len)
  1103.      char *envar;
  1104.      unsigned int len;
  1105. {
  1106.   struct variable *v;
  1107.   register char *args;
  1108.   register struct command_switch *cs;
  1109.  
  1110.   v = lookup_variable (envar, len);
  1111.   if (v == 0 || *v->value == '\0')
  1112.     return;
  1113.  
  1114.   for (args = v->value; *args != '\0'; ++args)
  1115.     for (cs = switches; cs->c != '\0'; ++cs)
  1116.       if (cs->c == *args)
  1117.     if (cs->env)
  1118.       switch (cs->type)
  1119.         {
  1120.         case string:
  1121.           /* None of these allow environment changes.  */
  1122.         default:
  1123.           abort ();
  1124.         case flag:
  1125.         case flag_off:
  1126.           *(int *) cs->value_ptr = cs->type == flag;
  1127.           break;
  1128.         case positive_int:
  1129.           while (isspace (args[1]))
  1130.         ++args;
  1131.           if (isdigit(args[1]))
  1132.         {
  1133.           int i = atoi (&args[1]);
  1134.           while (isdigit (args[1]))
  1135.             ++args;
  1136.           if (i >= 1)
  1137.             *(unsigned int *) cs->value_ptr = i;
  1138.         }
  1139.           else
  1140.         *(unsigned int *) cs->value_ptr
  1141.           = *(unsigned int *) cs->noarg_value;
  1142.           break;
  1143.         case floating:
  1144.           while (isspace (args[1]))
  1145.         ++args;
  1146.           if (args[1] == '.' || isdigit (args[1]))
  1147.         {
  1148.           *(double *) cs->value_ptr = atof (&args[1]);
  1149.           while (args[1] == '.' || isdigit (args[1]))
  1150.             ++args;
  1151.         }
  1152.           else
  1153.         *(double *) cs->value_ptr = *(double *) cs->noarg_value;
  1154.           break;
  1155.         }
  1156. }
  1157.  
  1158. /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
  1159.    command switches.  Include positive_int and floating options if PF.
  1160.    Don't include options with the `no_makefile' flag is if MAKEFILE.  */
  1161.  
  1162. static void
  1163. define_makeflags (pf, makefile)
  1164.      int pf, makefile;
  1165. {
  1166.   register struct command_switch *cs;
  1167.   char flags[200];
  1168.   register unsigned int i;
  1169.  
  1170.   i = 0;
  1171.   for (cs = switches; cs->c != '\0'; ++cs)
  1172.     if (cs->toenv && (!makefile || !cs->no_makefile))
  1173.       {
  1174.     if (i == 0 || flags[i - 1] == ' ')
  1175.       flags[i++] = '-';
  1176.     switch (cs->type)
  1177.       {
  1178.       default:
  1179.         abort ();
  1180.  
  1181.       case ignore:
  1182.         break;
  1183.  
  1184.       case flag:
  1185.       case flag_off:
  1186.         if (!*(int *) cs->value_ptr == (cs->type == flag_off)
  1187.         && (cs->default_value == 0
  1188.             || *(int *) cs->value_ptr != *(int *) cs->default_value))
  1189.           flags[i++] = cs->c;
  1190.         break;
  1191.  
  1192.       case positive_int:
  1193.         if (pf)
  1194.           {
  1195.         if ((cs->default_value != 0
  1196.              && (*(unsigned int *) cs->value_ptr
  1197.              == *(unsigned int *) cs->default_value)))
  1198.           break;
  1199.         else if (cs->noarg_value != 0
  1200.              && (*(unsigned int *) cs->value_ptr ==
  1201.                  *(unsigned int *) cs->noarg_value))
  1202.           flags[i++] = cs->c;
  1203.         else
  1204.           {
  1205.             unsigned int value;
  1206.             if (cs->c == 'j')
  1207.               value = 1;
  1208.             else
  1209.               value = *(unsigned int *) cs->value_ptr;
  1210.             sprintf (&flags[i], "%c%u ", cs->c, value);
  1211.             i += strlen (&flags[i]);
  1212.           }
  1213.           }
  1214.         break;
  1215.  
  1216.       case floating:
  1217.         if (pf)
  1218.           {
  1219.         if (cs->default_value != 0
  1220.             && (*(double *) cs->value_ptr
  1221.             == *(double *) cs->default_value))
  1222.           break;
  1223.         else if (cs->noarg_value != 0
  1224.              && (*(double *) cs->value_ptr
  1225.                  == *(double *) cs->noarg_value))
  1226.           flags[i++] = cs->c;
  1227.         else
  1228.           {
  1229.             sprintf (&flags[i], "%c%f ",
  1230.                  cs->c, *(double *) cs->value_ptr);
  1231.             i += strlen (&flags[i]);
  1232.           }
  1233.           }
  1234.         break;
  1235.       }
  1236.       }
  1237.  
  1238.   if (i == 0)
  1239.     flags[0] = flags[1] = '\0';
  1240.   else if (flags[i - 1] == ' ' || flags[i - 1] == '-')
  1241.     flags[i - 1] = '\0';
  1242.   flags[i] = '\0';
  1243.  
  1244.   /* On Sun, the value of MFLAGS starts with a `-' but the
  1245.      value of MAKEFLAGS lacks the `-'.  Be compatible.  */
  1246.   (void) define_variable ("MAKEFLAGS", 9, &flags[1], o_env, 0);
  1247.   (void) define_variable ("MFLAGS", 6, flags, o_env, 0);
  1248. }
  1249.  
  1250. static int printed_version = 0;
  1251.  
  1252. /* Print version information.  */
  1253.  
  1254. static void
  1255. print_version ()
  1256. {
  1257.   extern char *remote_description;
  1258.   char *precede = print_data_base_flag ? "# " : "";
  1259.  
  1260.   printf ("%sGNU Make version %s", precede, version_string);
  1261.   if (remote_description != 0 && *remote_description != '\0')
  1262.     printf ("-%s", remote_description);
  1263.  
  1264.   printf (", by Richard Stallman and Roland McGrath.\n\
  1265. %sCopyright (C) 1988-1991 Free Software Foundation, Inc.\n\
  1266. %sThis is free software; see the source for copying conditions.\n\
  1267. %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
  1268. %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
  1269.  
  1270.   printed_version = 1;
  1271.  
  1272.   /* Flush stdout so the user doesn't have to wait to see the
  1273.      version information while things are thought about.  */
  1274.   fflush (stdout);
  1275. }
  1276.  
  1277. /* Print a bunch of information about this and that.  */
  1278.  
  1279. static void
  1280. print_data_base ()
  1281. {
  1282.   extern char *ctime ();
  1283.   time_t when;
  1284.  
  1285.   when = time ((time_t *) 0);
  1286.   printf ("\n# Make data base, printed on %s", ctime (&when));
  1287.  
  1288.   print_variable_data_base ();
  1289.   print_dir_data_base ();
  1290.   print_rule_data_base ();
  1291.   print_file_data_base ();
  1292.   print_vpath_data_base ();
  1293.  
  1294.   when = time ((time_t *) 0);
  1295.   printf ("\n# Finished Make data base on %s\n", ctime (&when));
  1296. }
  1297.  
  1298. /* Exit with STATUS, cleaning up as necessary.  */
  1299.  
  1300. void
  1301. die (status)
  1302.      int status;
  1303. {
  1304.   static char dying = 0;
  1305.  
  1306.   if (!dying)
  1307.     {
  1308.       dying = 1;
  1309.  
  1310.       if (print_version_flag && !printed_version)
  1311.     print_version ();
  1312.  
  1313.       /* Wait for children to die.  */
  1314.       wait_for_children (0, status != 0);
  1315.  
  1316.       /* Remove the intermediate files.  */
  1317.  
  1318.       remove_intermediates (0);
  1319.  
  1320.       if (print_directory_flag)
  1321.     log_working_directory (0);
  1322.     }
  1323.  
  1324.   exit (status);
  1325. }
  1326.  
  1327. /* Write a message indicating that we've just entered or
  1328.    left (according to ENTERING) the current directory.  */
  1329.  
  1330. static void
  1331. log_working_directory (entering)
  1332.      int entering;
  1333. {
  1334.   static int entered = 0;
  1335.   char pwdbuf[MAXPATHLEN];
  1336.   char *message = entering ? "Entering" : "Leaving";
  1337.  
  1338.   if (entered && entering)
  1339.     /* Don't print the leaving message if we
  1340.        haven't printed the entering message.  */
  1341.     return;
  1342.   else
  1343.     entered = 1;
  1344.  
  1345.   if (print_data_base_flag)
  1346.     fputs ("# ", stdout);
  1347.  
  1348.   if (makelevel == 0)
  1349.     printf ("%s: %s ", program, message);
  1350.   else
  1351.     printf ("%s[%u]: %s ", program, makelevel, message);
  1352.  
  1353. #ifdef    USG
  1354.   /* In some System V's, `getcwd' spawns a child running /bin/pwd.  */
  1355.   push_signals_blocked_p (1);
  1356. #endif
  1357.   if (getwd (pwdbuf) == 0)
  1358.     printf ("an unknown directory (getwd: %s)\n", pwdbuf);
  1359.   else
  1360.     printf ("directory `%s'\n", pwdbuf);
  1361. #ifdef    USG
  1362.   pop_signals_blocked_p ();
  1363. #endif
  1364. }
  1365.